# common.py - for pretty much everything else.

# common date processing for all reports
# defaults to the last 12 weeks
def process_report_dates(dt_start,dt_end):
	"""Processes start and end date strings for reports, outputs two datetime objs.
	
	Defaults end date to today + 1d (because today() will ignore anything created after midnight)
	Defaults start date to tomorrow - 12 weeks
	"""
	from datetime import date, timedelta
	if dt_end == None:	
		dt_end = date.today() + timedelta(days=1)
	else:
		dt_end = dt_end.split('-')
		dt_end = date(dt_end[2],dt_end[1],dt_end[0])
		
	if dt_start == None:
		dt_start = dt_end - timedelta(weeks=12)
		
	return {'dt_start': dt_start, 'dt_end': dt_end}

def process_filter_ids(tech_id=None, site_id=None):
	"""Accepts tech and site id's and returns their assoc. objs for filtering""" 
	try:
		if tech_id != None:
			t = User.objects.get(pk=tech_id)
		else: 
			t = None
		if site_id != None:
			s = Site.objects.get(site_id=site_id)
		else:
			s = None
	except (KeyError, User.DoesNotExist, Site.DoesNotExist):
		return None
		
	return { 'tech': t, 'site': s }

def process_filter_techname(name):
	"""Accepts an underscore delimited first/last name combo and spits out its matching User."""
	from django.contrib.auth.models import User
	name = name.split('_')
	l_name = ' '.join(name[1:])
	return User.objects.get(first_name=name[0],last_name=l_name)
	
class Notification():
	"""Provides a common class for handling notifications. Accepts a string message and a type.
	
	Type can be:
		ERROR
		SUCCESS
		INFO
		WARNING
	"""
	ERROR = "error"
	SUCCESS = "success"
	INFO = "info"
	WARNING = "warning"

	ERROR_GET_POST = "An error occurred while reading submitted data. Please check your data and try again."
	ERROR_GET_SITE = "An error occurred while retrieving information on the specified site. Please try again."
	ERROR_GET_TECH = "Could not retrieve any users matching the data provided. Please check your info and try again."
	ERROR_GET_TICKET = "Could not retreive any tickets based on the data provided. Please check your data and try again."
	ERROR_GET_GENERIC = "Could not retreive information using the data provided. Please try again."
	
	ERROR_SAVE_CHECKIN = "An error occurred while saving your check in. Please try again."
	ERROR_SAVE_TICKET = "An error occurred while saving your support ticket. Please check your data and try again."
	
	SUCCESS_SAVE_CHECKIN = "Your current location has been updated. Thank you!"
	SUCCESS_SAVE_TICKET = "Your ticket has been submitted. Thank you!"
	
	message = "Something happened but some silly coder didn't put in a message so... yeah."
	type = INFO
	
	def __init__(self, message, type=None):
		self['message'] = message
		if type:
			self['type'] = type
		